package irri.soils.ssnm;

import irri.soils.ssnm.R;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ActivityA extends SSNM implements View.OnClickListener{
    /** Called when the activity is first created. */
	TextView tv;
	Cursor c;
	private Bundle b;
	private Intent intent;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        setCustomTitle();
        Button lang1 = (Button) findViewById(R.id.option1);
        lang1.setOnClickListener(this);
        Button lang2 = (Button) findViewById(R.id.option2);
        lang2.setOnClickListener(this);
        Button lang3 = (Button) findViewById(R.id.option3);
        lang3.setOnClickListener(this);
        Button lang4 = (Button) findViewById(R.id.option4);
        lang4.setOnClickListener(this);
        Button lang5 = (Button) findViewById(R.id.option5);
        lang5.setOnClickListener(this);
        Button lang6 = (Button) findViewById(R.id.option6);
        lang6.setOnClickListener(this);
        SQLiteDatabase myDB= null;
       
        try {
         myDB = this.openOrCreateDatabase("SSNM2", MODE_WORLD_WRITEABLE, null);
         myDB.execSQL("CREATE TABLE IF NOT EXISTS lang (lang INT(3));");
         Cursor c = myDB.rawQuery("SELECT count(*) FROM lang", null);
         if (c != null) {
             c.moveToFirst();
             if(c.getInt(0) == 1){
                 intent = new Intent(ActivityA.this,
          				SSNM.prevActivity==null||
          				this.getIntent().getExtras()==null
          				? ActivityMain5.class : 
          					SSNM.prevActivity.getClass());
            	 Cursor c2 = myDB.rawQuery("SELECT * FROM lang" , null);
            	 
            	 if(this.getIntent().getExtras()!=null)
            		 b = this.getIntent().getExtras();
            	 else b = new Bundle();
            	 
         		 if(c2 != null){
            		 c2.moveToFirst();
             		 b.putInt("lang", c2.getInt(0));
             		 SSNM.d = c2.getInt(0) - 1;
             		 c2.close();
            	 }
         		intent.putExtras(b);
             }else{
            	 if(this.getIntent().getExtras()!=null){
            		 b = this.getIntent().getExtras();
            		 myDB.execSQL("INSERT INTO lang (lang) VALUES ("+b.getInt("lang")+");");
            	 }
            }
             c.close();
         }         
        }
        catch(Exception e) {
         Log.e("Error", "Error", e);
        } finally {
        	
         if (myDB != null)
          myDB.close();
        }
        if(intent!=null){ 
        	intent.putExtras(b);
        	startActivity(intent);
        	prevActivity = null;
        	finish();
        }
        
    }
    
	@Override
	public void onClick(View v) {
		int option = 0;
		switch(v.getId()){
			case R.id.option1: option = 1; break;
			case R.id.option2: option = 2; break;
			case R.id.option3: option = 3; break;
			case R.id.option4: option = 4; break;
			case R.id.option5: option = 5; break;
			case R.id.option6: option = 6; break;
		}
		
		SQLiteDatabase myDB= null;
		tv = (TextView) findViewById(R.id.textView1);
		try {
		 myDB = this.openOrCreateDatabase("SSNM2", MODE_WORLD_WRITEABLE, null);
		 myDB.execSQL("CREATE TABLE IF NOT EXISTS lang (lang INT(3));");
		 myDB.execSQL("DELETE FROM lang;");
	     myDB.execSQL("INSERT INTO lang (lang) VALUES ("+option+");");
	     //SSNMActivity.prevActivity.getClass()
	     intent = new Intent(ActivityA.this, SSNM.prevActivity==null ? 
	    		 ActivityMain5.class : SSNM.prevActivity.getClass());
	     if(this.getIntent().getExtras()!=null) b = this.getIntent().getExtras();
    	 else b = new Bundle();
	     b.putInt("lang", option);
	     SSNM.d = option - 1;
 		 intent.putExtras(b);
		}catch(Exception e) {
         tv.setText(e.toString());
        } finally {
         if (myDB != null)
          myDB.close();
        }
        if(intent!=null){
        	intent.putExtras(b);
        	startActivity(intent);
        }
        prevActivity = null;
        finish();
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return false;
    }

	@Override
	public void onBackPressed() {
		if(SSNM.prevActivity!=null){
			intent = new Intent(ActivityA.this, SSNM.prevActivity.getClass());
		    if(this.getIntent().getExtras()!=null) b = this.getIntent().getExtras();
		    else b = new Bundle();
		    intent.putExtras(b);
		    startActivity(intent);
		    prevActivity = null;
		}
		//System.out.println("testing");
	    finish();
	}
}